<?php
/**
 * @文件功能名称  : 用户模型
 * @Author      : yaoGrace
 * @Date        : 2021/11/28 18:51
 */
namespace phpGrace\models;
class manager extends \graceModel{
    // 表名
    public $tableName ="manager_user";
    //主键
    public  $tableKey ="id";

    /**
     * 添加数据，返回添加成功的id
     * @param $data
     * @return intID
     */
    public function add($data){
        return $this->m->add($data);
    }

    /**
     * 通过id进行数据更新
     * @param $id
     * @param $data
     */
    public function change($id,$data){
        $this->m->where('id = ?',array($id))->update($data);
    }

    /**
     * 通过id进行数据删除
     * @param $id
     * @return bool
     */
    public function del($id){
        return $this->m->where("id = ? ",array($id))->delete();
    }

    /**
     * 通过id 查询数据，返回数组
     * @param $id
     * @return 数组
     */
    public function checkAuthByid($id){
       return $this->m->where("id = ? ",array($id))->fetch() ;
    }

    /**
     * 通过邮箱查询是否有重复的,大于0 则重复
     * @param $email
     * @return int
     */
    public function checkEmailNum($email){
        return $this->m->where('email = ? ',array($email))->count();
    }

    /**
     * 返回当前用户的登录信息 system/index 用到
     * @param $ManageID
     * @return array
     */
    public function getLoginDataByManageID($ManageID){
        return $this->m
            ->join('as a left  join '.sc('db','pre').'manager_roles as b on a.manager_role_id = b.role_id')
            ->where("a.id = ? ",array($ManageID))
            ->fetchAll('a.id,a.email,a.manager_login_time,a.manager_uname,a.manager_ip,b.role_name');
    }

    /**
     * 返回mysql版本
     * @return string
     */
    public function getMysqlVersion(){
        return $this->m->mysqlV();
    }

    public function getAllList(){
        $items =  $this->m
            ->order('a.id asc')
            ->join('as a left  join '.sc('db','pre').'manager_roles as b on a.manager_role_id = b.role_id')
            ->page(10)
            ->fetchAll('a.id,a.manager_uname,a.email,a.manager_login_time,a.create_time,a.manager_ip,b.role_name');
        return $items;
    }

    public function searchWhere($getSearchEmail,$getSearchRoleid)
    {
        //判断使用的是用email 方式搜索 还是 用角色搜索
        if($getSearchEmail!= '0'  &&  $getSearchRoleid == '-1'){
            //email 有数据  角色无数据
            $where = "a.email = ? " ;
            $whereDataArray = array($getSearchEmail);
        }elseif ($getSearchEmail =='0' &&  $getSearchRoleid!='-1'){
            //email 无数据  角色有数据
            $where ="a.manager_role_id = ? ";
            $whereDataArray = array($getSearchRoleid);
        }elseif($getSearchEmail =='0' &&  $getSearchRoleid=='-1'){
            //email 无数据 ，角色 无数据
            $where =" ''='' ";
            $whereDataArray = '';
        }elseif($getSearchEmail!='0'  && $getSearchRoleid!='-1' ){
            //email 有数据 角色有数据
            $where =" a.email = ?  and a.manager_role_id = ?  ";
            $whereDataArray = array($getSearchEmail,$getSearchRoleid );
        }
        $items =  $this->m
            ->order('a.id asc')
            ->join('as a left  join '.sc('db','pre').'manager_roles as b on a.manager_role_id = b.role_id')
            ->where($where,$whereDataArray)
            ->page(10)
            ->fetchAll('a.id,a.manager_uname,a.email,a.manager_login_time,a.create_time,a.manager_ip,b.role_name');
        return $items;
    }
}